package com.salemanagement.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//创建交换机和队列以及绑定关系
@Configuration
public class RabbitConfig {
    public String queueName03="dead_queue03";
    public String exchangeName03="dead_exchange03";
    public String exchangeName01="pt_exchange01";
    public String queueName01="pt_queue01";
    //创建队列--交于spring管理
    @Bean //把该方法的返回对象交于spring容器管理对象的生命周期。
    public Queue pt_queue01(){
        return QueueBuilder.durable(queueName01)
                .ttl(100000)
                .deadLetterExchange(exchangeName03)
                .deadLetterRoutingKey("dead.weixin")
//                .withArgument("x-message-ttl", 30000)          // 消息存活时间(ms)
//                .withArgument("x-dead-letter-exchange", exchangeName03) // 死信交换机
//                .withArgument("x-dead-letter-routing-key", "dead.weixin")      // 死信路由键
                .build();
    }

//    死信队列
    @Bean
    public Queue dead_queue03(){
        Queue queue = QueueBuilder
                .durable(queueName03)
                .build();
        return queue;
    }
//    死信交换机
    @Bean
    public Exchange exchange03(){
        Exchange exchange = ExchangeBuilder.topicExchange(exchangeName03).durable(true).build();
        return exchange;
    }
    //创建交换机
    @Bean
    public Exchange exchange01(){
        Exchange exchange = ExchangeBuilder.topicExchange(exchangeName01).durable(true).build();
        return exchange;
    }

    //绑定关系
    @Bean
    public Binding binding01(){
        Binding binding = BindingBuilder.bind(pt_queue01()).to(exchange01()).with("dead.#").noargs();
        return binding;
    }
//    死信队列和死信交换机绑定关系
@Bean
public Binding deadBinding() {
    return BindingBuilder
            .bind(dead_queue03())
            .to(exchange03())
            .with("dead.#")
            .noargs();
}


    //注意: 如果交换机和队列绑定 每个绑定都需要独立编写一个绑定关系
    //发消息时创建队列和交换机
}
